package br.com.rglariguet.pioneer.controller.asc;

import java.io.File;
import java.io.FileInputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import br.com.rglariguet.pioneer.controller.ControllerComum;
import br.com.rglariguet.pioneer.dao.ManualDAO;
import br.com.rglariguet.pioneer.modelos.Manual;

@Controller
@RequestMapping("/seguro/asc/")
public class ManualController extends ControllerComum {

	List<Manual> listaDeManuais = new ArrayList<Manual>();

	@Autowired
	private ManualDAO manualDao;
	
	@Autowired
	private ServletContext servletContext;

	@RequestMapping("manuaisProdutos")
	public ModelAndView abreTelaManuaisProdutos(String nome) {
		ModelAndView mv = new ModelAndView("/seguro/asc/manuaisProdutos");
		populaListaManuaisAutoComplete();
		if (nome == null) {
			mv.addObject("listaManuais", manualDao.listarTodos());
		} else {
			mv.addObject("listaManuais", manualDao.buscaPorNome(nome));
		}
		return mv;
	}

	private void populaListaManuaisAutoComplete() {
		if (listaDeManuais.isEmpty()) {
			for (Manual manual : manualDao.listarTodos()) {
				listaDeManuais.add(manual);
			}
		}
	}

	@RequestMapping("consultaManual")
	public ModelAndView consultaManual(long codigo, HttpServletResponse response) {
		Manual manual = manualDao.buscaPorCodigo(codigo);
		String pdfFileName = manual.getNomeArquivo() + ".pdf";
		String contextPath = servletContext.getRealPath("/WEB-INF//manuais");
		File pdfFile = new File(contextPath + "/" + pdfFileName);

		response.setContentType("application/pdf");
		response.addHeader("Content-Disposition", "attachment; filename=" + pdfFileName);
		response.setContentLength((int) pdfFile.length());

		FileInputStream fileInputStream;
		try {
			fileInputStream = new FileInputStream(pdfFile);

			OutputStream responseOutputStream = response.getOutputStream();
			int bytes;
			while ((bytes = fileInputStream.read()) != -1) {
				responseOutputStream.write(bytes);
			}
			fileInputStream.close();
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {

		}
		return abreTelaManuaisProdutos(null);
	}

	@RequestMapping(value = "getManuais", method = RequestMethod.GET)
	public @ResponseBody List<Manual> getTags(@RequestParam String nome) {
		return simulaBuscaAutocomplete(nome);
	}

	private List<Manual> simulaBuscaAutocomplete(String nome) {
		List<Manual> result = new ArrayList<Manual>();
		for (Manual manual : listaDeManuais) {
			if (manual.getNome().toLowerCase().contains(nome.toLowerCase())) {
				result.add(manual);
			}
		}
		return result;
	}

}
